<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图书列表</title>
    <style>
        input[type="submit"],.button-link {
            width: 100%;
            padding: 10px;
            border: none;
            border-radius: 4px;
            background-color: #4CAF50;
            color: white;
            cursor: pointer;
            text-decoration: none;
            display: inline-block;
            text-align: center;
        }

        input[type="submit"]:hover,.button-link:hover {
            background-color: #45a049;
        }

        .button-container {
            text-align: center;
            margin-top: 15px;
        }

        body, html {
            height: 100%;
            margin: 0;
            font-family: Arial, sans-serif;
        }

        .sidebar {
            width: 90px;
            background-color: #333;
            color: white;
            padding: 15px;
            position: fixed;
            top: 0;
            left: 0;
            height: 100%;
        }

        .sidebar h2 {
            padding: 10px;
            color: #fff;
        }

        .sidebar ul {
            list-style-type: none;
            padding: 0;
        }

        .sidebar ul li a {
            color: white;
            padding: 10px;
            display: block;
            text-decoration: none;
        }

        .sidebar ul li a:hover {
            background-color: #575757;
        }

        .sidebar ul li a.active {
            background-color: #4CAF50; /* 设置当前页面的背景颜色 */
        }

        .main-content {
            margin-left: 120px;
            padding: 20px;
        }

        .header {
            background-color: #333;
            color: white;
            text-align: center;
            padding: 10px 0;
        }

        .content {
            padding: 20px;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }

        th, td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }

        th {
            background-color: #f2f2f2;
        }
    </style>
</head>
<body>
<div class="sidebar">
    <h2>菜单</h2>
    <ul>
        <li><a href="bookList.jsp" class="<%= request.getRequestURI().endsWith("bookList.jsp")? "active" : "" %>">图书列表</a></li>
        <li><a href="addBook.jsp" class="<%= request.getRequestURI().endsWith("addBook.jsp")? "active" : "" %>">添加图书</a></li>
        <li><a href="borrowRecord.jsp" class="<%= request.getRequestURI().endsWith("borrowRecord.jsp")? "active" : "" %>">借阅记录</a></li>
        <li><a href="readerList.jsp" class="<%= request.getRequestURI().endsWith("readerList.jsp")? "active" : "" %>">读者列表</a></li>
        <li id="adminLink" style="display: none;"><a href="bookManagement.jsp" class="<%= request.getRequestURI().endsWith("bookManagement.jsp")? "active" : "" %>">管理入口</a></li>
        <li><a href="PersonalData.jsp" class="<%= request.getRequestURI().endsWith("PersonalData.jsp")? "active" : "" %>">个人中心</a></li>
        <li><a href="<%=request.getContextPath()%>/login.jsp">退出登录</a></li>
    </ul>
</div>
<div class="main-content">
    <div class="header">
        <a>图书信息管理系统 - 图书列表</a>
    </div>
    <div class="content">
        <h2>图书列表</h2>
        <a href="addBook.jsp">添加图书</a>
        <table>
            <thead>
            <tr>
                <th>图书ID</th>
                <th>书名</th>
                <th>作者</th>
                <th>出版社</th>
                <th>出版年份</th>
                <th>ISBN</th>
                <th>价格</th>
                <th>描述</th>
                <th>分类ID</th>
                <th>库存数量</th>
                <th>可用数量</th>
            </tr>
            </thead>
            <tbody>
            <%@ page import="com.wzu.pojo.Book" %>
            <%@ page import="com.wzu.service.BookService" %>
            <%@ page import="com.wzu.service.BookServiceImpl" %>
                <%
            BookService bookService = new BookServiceImpl();
            java.util.List<Book> bookList = bookService.getAllBooks();
            for (Book book : bookList) {
        %>
            <tr>
                <td><%= book.getBook_id() %></td>
                <td><%= book.getTitle() %></td>
                <td><%= book.getAuthor() %></td>
                <td><%= book.getPublisher() %></td>
                <td><%= book.getPublication_year() %></td>
                <td><%= book.getIsbn() %></td>
                <td><%= book.getPrice() %></td>
                <td><%= book.getDescription() %></td>
                <td><%= book.getCategory_id() %></td>
                <td>
                    <input type="number" value="<%= book.getStock_quantity() %>" 
                           name="stock_<%= book.getBook_id() %>" 
                           style="width: 60px;">
                </td>
                <td>
                    <input type="number" value="<%= book.getAvailable_quantity() %>"
                           name="available_<%= book.getBook_id() %>" 
                           style="width: 60px;">
                </td>
            </tr>
                <%
            }
        %>
    </div>
</div>
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
<script>
    // 读取信息
    let userInfo = sessionStorage.getItem('userInfo');
    userInfo = userInfo? JSON.parse(userInfo) : null;
    console.log(userInfo);

    // 根据 isroot 的值动态显示/隐藏 "管理入口"
    if (userInfo && userInfo.isroot === 0) {
        document.getElementById('adminLink').style.display = 'none';
    } else {
        document.getElementById('adminLink').style.display = 'list-item';
    }
</script>
</body>
</html>

<div class="submit-container" style="
    position: fixed;
    bottom: 20px;
    right: 20px;
    z-index: 1000;">
    <form id="stockForm" action="<%=request.getContextPath()%>/UpdateStock" method="post">
        <input type="submit" value="批量更新库存" 
               style="padding: 10px 20px;
                      background-color: #4CAF50;
                      color: white;
                      border: none;
                      border-radius: 4px;
                      cursor: pointer;">
    </form>
</div>

<script>
document.getElementById('stockForm').addEventListener('submit', function(e) {
    const inputs = document.querySelectorAll('input[type="number"]');
    inputs.forEach(input => {
        if (isNaN(input.value) || input.value < 0) {
            alert('请输入有效的数字');
            e.preventDefault();
            return;
        }
    });

    // 收集所有修改数据
    const formData = [];
    document.querySelectorAll('tr[data-book-id]').forEach(row => {
        const bookId = row.dataset.bookId;
        formData.push({
            id: bookId,
            stock: row.querySelector('input[name^="stock"]').value,
            available: row.querySelector('input[name^="available"]').value
        });
    });
    
    // 转换为JSON格式提交
    const hiddenInput = document.createElement('input');
    hiddenInput.type = 'hidden';
    hiddenInput.name = 'stockData';
    hiddenInput.value = JSON.stringify(formData);
    this.appendChild(hiddenInput);
});
</script>